layui.use([
    'element',
    'form',
    'layer',
    'table',
    'upload',
], function() {
    $(function () {
        new Match();
    });
    function Match() {
        /**
         * 成员属性
         * @type {string}
         */
        this.urls={
            previewUrl:CommonUtils.siteName+'images/excel.png',
            executeMatchUrl:CommonUtils.siteName+'gdp/regionGdp/excImport',
            findGdpByYear:CommonUtils.siteName+'gdp/regionGdp/findGdpByYear'
        };
        this.upFileId=null;//当前上传文件ID
        this.selectedYear=null;//选中的类型

        /**
         * 获取素有通用的元素
         * (建议通过function的方式返回，防止因为缓存的原因导致元素事件无法激活)
         * @returns
         */
        this.elements={
        };

        /**
         * 初始化函数
         */
        this.init=function () {
            this.initUI();
            this.initOpt();
            this.initUIEvent();
        };

        /**
         * 成员方法
         */
        this.initUI=function () {
            var that=this;
            //执行一个laydate实例
            layui.laydate.render({
                elem: '#impYear' //指定元素
                ,type:'year'
                ,showBottom: false
                ,change: function(value, date, endDate){
                    // 获取翻页或点击选择年份数据
                    // var iValue = parseInt(value);
                    that.selectedYear=value;
                    $("#impYear").val(value);
                }
                ,ready : function(date) {
                    $(".layui-laydate").off('click').on('click', '.layui-laydate-list li', function() {
                        $(".layui-laydate").remove();
                    });
                }

            });
        };
        this.initOpt=function () {
        };
        this.initUIEvent=function () {
            var that=this;
            /**
             * 开始项目匹配
             */
            $(".ajax-import").click(function () {
                // if (that.selectedYear==null){
                //     layer.msg("年份不能为空");
                //     return;
                // }
                if (that.upFileId==null){
                    layer.msg("请上传文件数据");
                    return;
                }
                that.saveGdpData();
                // that.findGdpByYear(function (rows) {
                //     if (rows.length>0){//年份数据已经存在，提示是否覆盖更新
                //         layer.confirm('您选择导入的年份GDP数据已存在，是否覆盖？', function(index){
                //             //do something
                //             that.saveGdpData();
                //             layer.close(index);
                //         });
                //     }else that.saveGdpData();//如果年份数据为空，则直接导入
                //
                // });

            });
            /**
             * 文件上传组件初始化
             */
            layui.upload.render({
                elem: '.upload-prjFile' //绑定元素
                , url: $('.upload-prjFile').attr('up-url') //上传接口
                , field: 'prjFile' //文件域的字段名
                , acceptMime: 'application/vnd.ms-excel' //选择文件类型
                , exts: 'xls' //支持的图片格式
                , multiple: true //开启多文件选择
                , choose: function (obj) {
                    obj.preview(function (index, file, result) {
                        var upload = $('.upload-prjFile');
                        // var previewUrl=upload.attr('preview-url')
                        // var name = upload.attr('name');
                        var show = upload.parents('.layui-form-item').children('.upload-show');
                        show.append("<div class='upload-item'><img style='height: 64px;width: 64px;' src='"+that.urls.previewUrl+"'/>" +
                            "<input id='" + index + "' type='hidden' name='fileId'/>" +
                            "<i class='upload-item-close layui-icon layui-icon-close'></i></div>");
                    });
                }
                , done: function (res, index, upload) {
                    that.upFileId=res.data.id;
                    $("input[name='fileId']").val(res.data.id);
                }
            });
        };
        /**
         * 刷新数据表格
         */
        this.reloadProjectList=function () {
            layui.table.reload('projTable', {})
        };
        /**
         * 根据年份查询GDP数据
         */
        this.findGdpByYear=function (onSucceed) {
            var that=this;
            var param={
                year:that.selectedYear,
            };
            $.post(that.urls.findGdpByYear,param, function (result) {
                if (result.code === 200) {
                    if (typeof onSucceed=="function"){
                        onSucceed(result.data);
                    }
                } else {
                    layer.alert(result.msg,{icon: 2});
                }
            });

        };
        /**
         * 覆盖更新GDP年份数据
         */
        this.saveGdpData=function () {
            var that=this;
            //开始更新GDP数据
            var param={
                upFileId:that.upFileId
            };
            $.post(that.urls.executeMatchUrl,param, function (result) {
                if (result.code === 200) {
                    layer.msg(result.msg, {offset: '15px', time: 3000, icon: 1});
                    setTimeout(function () {
                        parent.layer.close(window.parent.layerIndex);
                        window.parent.location.reload()
                    }, 2000);
                } else {
                    layer.alert(result.msg,{icon: 2});
                }
            });
        };

        //设置init为默认执行的函数
        this.init.apply(this, arguments);
    };
});






